<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */
 
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_warehouse.php');

if ($_REQUEST['act'] == 'change_is_valid')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_style_id=intval($_REQUEST['delivery_style_id']);

 	//判断仓库是否存在，是否已被删除
	$delivery_style_info=is_delivery_style_exists($delivery_style_id);
	if(!$delivery_style_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_style_not_exist'];
		die($json->encode($result));
	}
	else{
		
		$is_valid=$delivery_style_info['is_valid'];
		if($is_valid==1)
		{
			$sql="update ".$ecs->table('erp_delivery_style')." set is_valid='0' where delivery_style_id='".$delivery_style_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=0;
			die($json->encode($result));
		}
		elseif($is_valid==0)
		{
			$sql="update ".$ecs->table('erp_delivery_style')." set is_valid='1' where delivery_style_id='".$delivery_style_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=1;
			die($json->encode($result));			
		}
	}
}

elseif($_REQUEST['act'] == 'add_delivery_style')
{
  include('../includes/cls_json.php');
	$json  = new JSON;

	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
 
    $delivery_style=trim($_POST['delivery_style']);

    $sql="insert into ".$ecs->table('erp_delivery_style')." set delivery_style='".$delivery_style."',is_valid='1'";
	 
    $db->query($sql);
    $result['error']=0;
    die($json->encode($result));
}


elseif($_REQUEST['act'] == 'delete_delivery_style')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	
	$delivery_style_id=intval($_REQUEST['delivery_style_id']);

 	//判断入仓类型是否存在，是否已被删除
	$delivery_style_info=is_delivery_style_exists($delivery_style_id);
	if(!$delivery_style_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_style_not_exist'];
		die($json->encode($result));
	}
	else{
		
		//检查是否有出库单关联到这个出库类型，如果有，那么不能删除出库类型
		$sql="select count(*) as num from ".$ecs->table('erp_delivery')." where delivery_style_id='".$delivery_style_id."'";
		$num=$db->getOne($sql);
		
		if($num>0)
		{
			$result['error']=2;
			$result['message']=$_LANG['erp_delivery_style_has_delivery'];
			die($json->encode($result));			
		}			
		
		$result['error']=0;
		$sql="delete from ".$ecs->table('erp_delivery_style')." where delivery_style_id='".$delivery_style_id."'";
		$db->query($sql);

		die($json->encode($result));
	}
}


?>